package com.jthinker.blog.web.controller.admin.user;

import com.jthinker.blog.bean.vo.UserWrapper;
import com.jthinker.blog.service.UserInfoService;
import com.jthinker.blog.util.RSAUtils;
import com.jthinker.blog.web.controller.admin.common.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * User: Gang Chen
 * Date: 2015/8/18 19:17
 */
@Controller
@RequestMapping("/userMgr")
public class UserMgrController extends BaseController
{
    @Resource
    private UserInfoService userInfoService;

    @RequestMapping("/changePassword")
    @ResponseBody
    public Map<String, Object> changePassword(HttpServletRequest request, String oldPassword, String newPassword)
    {
        if (StringUtils.isBlank(oldPassword) || StringUtils.isBlank(newPassword))
        {
            return getErrorMessage("密码不能为空!");
        }
        UserWrapper current = userInfoService.getCurrentUser(request);

        oldPassword = RSAUtils.decryptStringByJs(oldPassword);
        newPassword = RSAUtils.decryptStringByJs(newPassword);

        int code = userInfoService.updatePassword(current, oldPassword, newPassword);

        String errorMsg;
        switch (code)
        {
            case -1:
                errorMsg = "用户不存在!这也行!?";
                break;
            case -2:
                errorMsg = "原密码不正确!";
                break;
            case -3:
                errorMsg = "系统异常!";
                break;
            case 1:
                return getSuccessMessage("修改成功!");
            default:
                errorMsg = "修改失败!";
                break;
        }

        return getErrorMessage(errorMsg);
    }
}
